package cn.magictodo.weblog.admin.event.subscriber.lucene;

import cn.magictodo.weblog.admin.event.DeleteArticleEvent;
import cn.magictodo.weblog.search.LuceneHelper;
import cn.magictodo.weblog.search.index.ArticleIndex;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.index.Term;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liu
 * @since 2023-12-11 20:51
 * 消费者
 */
@Component
@Slf4j
public class DeleteArticleSubscriber implements ApplicationListener<DeleteArticleEvent> {
    @Autowired
    private LuceneHelper luceneHelper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onApplicationEvent(DeleteArticleEvent event) {
        event.getArticleId();
        Term term = new Term(ArticleIndex.COLUMN_ID, event.getArticleId().toString());
        long count = luceneHelper.deleteIndex(ArticleIndex.NAME , term);
        log.info("==> 文章对应 Lucene 删除文档结束，articleId: {}，受影响行数: {}",event.getArticleId(),count);
    }
}
